<?php
try{
	include '../ini.php';
    include '../classes/xl_categories.php';
    $dt_xl_category=new xl_category;
    if(isset($_GET['id'])){
        $read=$dt_xl_category->read($_GET['id']);
        if($read === FALSE){
            throw new Exception('Mã này không tồn tại trên url');
        }
        if($_GET['id'] !=$read['id'] ){
            $_GET['id']=$dt_xl_admin->removeSQLI($_GET['id']);
            header("Location: categories_list.php?id=".$_GET['id']);
        }
    }
    $read_list=$dt_xl_category->read_list();
    $dt_smarty->assign('read_list',$read_list);
	$contenForLayout=$dt_smarty->fetch('posts/categories_list.tpl');
	$dt_smarty->assign('contenForLayout',$contenForLayout);
	$dt_smarty->assign('titleForLayout','Danh sách thể loại');
	$dt_smarty->display('layout/defaults.tpl');
    $dbh=NULL;
	
} catch(Exception $e){
    $dbh=NULL;
	$_SESSION['msg']=$e->getMessage();
	$_SESSION['type_msg']='error';
    if(empty($_SERVER['HTTP_REFERER'])){
        header('Location: categories_list.php');
    }else{
        header('Location:'.$_SERVER['HTTP_REFERER']);
    }
    
}
function print_name($list,$parent){
    foreach($list as $location):
        if($location['id'] == $parent){
            echo $location['category_name'];
        }else {
            echo ''; 
        }
    endforeach; 
}